<template>
	<view>
		<up-modal
			:show="inviteCodePopup.show"
			width="622rpx"
			:showConfirmButton="false"
			class="inviteCode_modal_box"
		>
			<view class="inviteCode_modal">
				<view class="inviteCode_modal_close">
					<u-icon name="close" color="#666666" size="23" @click="close"></u-icon>
				</view>
				<view class="inviteCode_modal_label">输入{{ text }}邀请码：</view>
				<up-input
					border="none"
					v-model="inviteCodePopup.value"
					:customStyle="{
						background: 'rgba(236, 236, 236, 0.8)',
						flexShrink: 0,
						padding: '16rpx 26rpx'
					}"
					fontSize="28rpx"
					color="#000000"
				></up-input>
				<view class="inviteCode_btn" @click="saveCode">确定</view>
			</view>
		</up-modal>
	</view>
</template>

<script setup>
import { ref, reactive, defineProps, defineEmits } from 'vue';
import HomeApi from '@/API/home/index.js';

//微信小程序上支持类型穿透
defineOptions({
	options: {
		styleIsolation: 'shared'
	}
});
const homeApi = new HomeApi();
let props = defineProps({
	text: {},
	type: {}
});
let emit = defineEmits(['query']);
//邀请码弹框
const inviteCodePopup = reactive({
	show: false,
	value: ''
});
/**
 * 保存邀请码
 */
const saveCode = () => {
	if (!inviteCodePopup.value) {
		uni.showToast({
			title: '请输入邀请码',
			icon: 'none'
		});
		return;
	}
	if (props.type == 'memorial') {
		homeApi
			.invitationJoinFFApi({
				invitedUserId: uni.getStorageSync('userInfo').userId,
				invitedCode: inviteCodePopup.value
			})
			.then((res) => {
				if (res.code == 0) {
					close();
					uni.showToast({
						title: '加入成功'
					});
				}
			});
	} else if (props.type == 'hall') {
		homeApi
			.inviteHallJoinApi({
				hallId: 0,
				userId: uni.getStorageSync('userInfo').userId,
				invitedCode: inviteCodePopup.value
			})
			.then((res) => {
				if (res.code == 0) {
					close();
					uni.showToast({
						title: res.msg
					});
				}
			});
	} else if (props.type == 'clan') {
		homeApi
			.inviteJoinGenealogyAdministrators({
				inviteUserId: uni.getStorageSync('userInfo').userId,
				genealogyInvitationCode: inviteCodePopup.value
			})
			.then((res) => {
				if (res.code == 0) {
					close();
					uni.showToast({
						title: res.msg
					});
				}
			});
	}
	emit('query');
};
const close = () => {
	inviteCodePopup.value = '';
	inviteCodePopup.show = false;
};
defineExpose({
	inviteCodePopup
});
</script>

<style lang="scss">
::v-deep .u-modal__content {
	padding: 0 !important;
}
.inviteCode_modal {
	display: flex;
	flex-direction: column;
	position: relative;
	width: 622rpx;
	background: #ffffff;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	padding: 72rpx 40rpx 48rpx 40rpx;

	.inviteCode_modal_close {
		position: absolute;
		right: 16rpx;
		top: 16rpx;
	}

	.inviteCode_modal_label {
		position: relative;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
		line-height: 38rpx;
		margin-bottom: 24rpx;
	}

	.inviteCode_btn {
		flex-shrink: 0;
		position: relative;
		margin: auto;
		margin-top: 48rpx;
		width: 180rpx;
		height: 60rpx;
		background: #f3e0bb;
		border-radius: 30rpx 30rpx 30rpx 30rpx;
		font-weight: 500;
		font-size: 28rpx;
		color: #524630;
		line-height: 60rpx;
		text-align: center;
	}
}
</style>
